﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuotationService.RealtimeQuotation
{
    internal class ComboEntityConverter :
        IEntityConverter<Quotation.Combo.BasicInfo, ComboBasicInfo>,
        IEntityConverter<Quotation.Combo.QuotationInfo, ComboQuotationInfo>,
        IEntityConverter<Quotation.Combo.TickInfo, ComboTickInfo>,
        IEntityConverter<Quotation.Combo.DailyData, ComboDailyData>,
        IEntityConverter<Quotation.Combo.MinuteData, ComboMinuteData>
    {
        public Quotation.Combo.BasicInfo Convert(ComboBasicInfo basicInfo)
        {
            var rtn = new Quotation.Combo.BasicInfo()
            {
                StkId = new Quotation.SymbolCode(basicInfo.Code),
                UserDefinedCode = basicInfo.UserDefinedCode,
                Name = basicInfo.Name,
                AuthCode = basicInfo.AuthCode,
                BasketType = basicInfo.BasketType,
                ContractTimes = basicInfo.ContractTimes,
                Decimal = basicInfo.Decimal,
                ExchangeStockCount = basicInfo.ExchangeStockCount,
                MaxOrderPrice = basicInfo.MaxOrderPrice,
                MinOrderPrice = basicInfo.MinOrderPrice,
                OpenPrice = basicInfo.OpenPrice,
                PreClosePrice = basicInfo.PreClosePrice,
                TracingIndex = new Quotation.SymbolCode(basicInfo.TracingIndex),
                UpdateTime = basicInfo.UpdateTime,
                QuotationTime = basicInfo.QuotationTime,
                Components = new List<Quotation.Combo.ComboComponent>()
            };

            if (basicInfo.Components != null)
            {
                foreach (var c in basicInfo.Components)
                {
                    rtn.Components.Add(Convert(c));
                }
            }

            return rtn;
        }

        private Quotation.Combo.ComboComponent Convert(ComboComponent component)
        {
            return new Quotation.Combo.ComboComponent()
            {
                Name = component.Name,
                ExecuteQty = component.ExecuteQty,
                StkId = new Quotation.SymbolCode(component.Code)

            };
        }

        public ComboBasicInfo Convert(Quotation.Combo.BasicInfo basicInfo)
        {
            var rtn = new ComboBasicInfo()
            {
                Code = basicInfo.StkId.LongCode,
                UserDefinedCode = basicInfo.UserDefinedCode,
                Name = basicInfo.Name,
                AuthCode = basicInfo.AuthCode,
                BasketType = basicInfo.BasketType,
                ContractTimes = basicInfo.ContractTimes,
                Decimal = basicInfo.Decimal,
                ExchangeStockCount = basicInfo.ExchangeStockCount,
                MaxOrderPrice = basicInfo.MaxOrderPrice,
                MinOrderPrice = basicInfo.MinOrderPrice,
                OpenPrice = basicInfo.OpenPrice,
                PreClosePrice = basicInfo.PreClosePrice,
                TracingIndex = basicInfo.TracingIndex.LongCode,
                UpdateTime = basicInfo.UpdateTime,
                QuotationTime = basicInfo.QuotationTime
            };

            if (basicInfo.Components != null)
            {
                List<ComboComponent> components = new List<ComboComponent>();
                foreach (var c in basicInfo.Components)
                {
                    components.Add(Convert(c));
                }
                rtn.Components = components.ToArray();
            }

            return rtn;
        }

        private ComboComponent Convert(Quotation.Combo.ComboComponent component)
        {
            return new ComboComponent()
            {
                Code = component.StkId.LongCode,
                ExchID = component.StkId.ExchId,
                Name = component.Name,
                ExecuteQty = component.ExecuteQty
            };
        }

        public Quotation.Combo.QuotationInfo Convert(ComboQuotationInfo quotInfo)
        {
            return new Quotation.Combo.QuotationInfo()
            {
                AuthCode = quotInfo.AuthCode,
                StkId =new Quotation.SymbolCode(quotInfo.Code),
                Name = quotInfo.Name,
                BuyPrice = quotInfo.BuyPrice,
                SellPrice = quotInfo.SellPrice,
                BuyAutoCostPrice = quotInfo.BuyAutoCostPrice,
                SellAutoCostPrice = quotInfo.SellAutoCostPrice,
                UpdateTime = quotInfo.UpdateTime,
                QuotationTime = quotInfo.QuotationTime,
                SN = quotInfo.SN,
                Tick = Convert(quotInfo.Tick)
            };
        }

        public ComboQuotationInfo Convert(Quotation.Combo.QuotationInfo quotInfo)
        {
            return new ComboQuotationInfo()
            {
                AuthCode = quotInfo.AuthCode,
                Code = quotInfo.StkId.LongCode,
                Name = quotInfo.Name,
                BuyPrice = quotInfo.BuyPrice,
                SellPrice = quotInfo.SellPrice,
                BuyAutoCostPrice = quotInfo.BuyAutoCostPrice,
                SellAutoCostPrice = quotInfo.SellAutoCostPrice,
                UpdateTime = quotInfo.UpdateTime,
                QuotationTime = quotInfo.QuotationTime,
                SN = quotInfo.SN,
                Tick = Convert(quotInfo.Tick)
            };
        }

        public Quotation.Combo.TickInfo Convert(ComboTickInfo tickInfo)
        {
            return new Quotation.Combo.TickInfo()
            {
                Diff = tickInfo.Diff,
                DiffRate = tickInfo.DiffRate,
                HighPrice = tickInfo.HighPrice,
                KnockAmt = tickInfo.KnockAmt,
                KnockAvgPrice = tickInfo.KnockAvgPrice,
                KnockPrice = tickInfo.KnockPrice,
                KnockQty = tickInfo.KnockQty,
                KnockTime = Quotation.Time.FromHMMss(tickInfo.KnockTime),
                LowPrice = tickInfo.LowPrice,
                TotalKnockAmt = tickInfo.TotalKnockAmt,
                TotalKnockQty = tickInfo.TotalKnockQty,
                TickSN = tickInfo.TickSN
            };
        }

        public ComboTickInfo Convert(Quotation.Combo.TickInfo tickInfo)
        {
            return new ComboTickInfo()
            {
                Diff = tickInfo.Diff,
                DiffRate = tickInfo.DiffRate,
                HighPrice = tickInfo.HighPrice,
                KnockAmt = tickInfo.KnockAmt,
                KnockAvgPrice = tickInfo.KnockAvgPrice,
                KnockPrice = tickInfo.KnockPrice,
                KnockQty = tickInfo.KnockQty,
                KnockTime = tickInfo.KnockTime.ToHMMss(),
                LowPrice = tickInfo.LowPrice,
                TotalKnockAmt = tickInfo.TotalKnockAmt,
                TotalKnockQty = tickInfo.TotalKnockQty,
                TickSN = tickInfo.TickSN
            };
        }

        public Quotation.Combo.DailyData Convert(ComboDailyData dailyData)
        {
            return new Quotation.Combo.DailyData()
            {
                StkId = new Quotation.SymbolCode(dailyData.Code),
                ClosePrice = dailyData.ClosePrice,
                HighPrice = dailyData.HighPrice,
                LowPrice = dailyData.LowPrice,
                OpenPrice = dailyData.OpenPrice,
                PriceBetaCoeff = dailyData.PriceBetaCoeff,
                PriceCorreCoeff = dailyData.PriceCorreCoeff,
                ReturnRateBetaCoeff = dailyData.ReturnRateBetaCoeff,
                ReturnRateCorreCoeff = dailyData.ReturnRateCorreCoeff,
                TotalKnockAmt = dailyData.TotalKnockAmt,
                TotalKnockQty = dailyData.TotalKnockQty,
                TradeDay = Quotation.Date.FromYMMDD(dailyData.TradeDay)
            };
        }

        public ComboDailyData Convert(Quotation.Combo.DailyData dailyData)
        {
            return new ComboDailyData()
            {
                Code = dailyData.StkId.LongCode,
                ClosePrice = dailyData.ClosePrice,
                HighPrice = dailyData.HighPrice,
                LowPrice = dailyData.LowPrice,
                OpenPrice = dailyData.OpenPrice,
                PriceBetaCoeff = dailyData.PriceBetaCoeff,
                PriceCorreCoeff = dailyData.PriceCorreCoeff,
                ReturnRateBetaCoeff = dailyData.ReturnRateBetaCoeff,
                ReturnRateCorreCoeff = dailyData.ReturnRateCorreCoeff,
                TotalKnockAmt = dailyData.TotalKnockAmt,
                TotalKnockQty = dailyData.TotalKnockQty,
                TradeDay = dailyData.TradeDay.ToYMMDD()
            };
        }

        public Quotation.Combo.MinuteData Convert(ComboMinuteData minuteData)
        {
            return new Quotation.Combo.MinuteData()
            {
                StkId = new Quotation.SymbolCode(minuteData.Code),
                ClosePrice = minuteData.ClosePrice,
                HighPrice = minuteData.HighPrice,
                LowPrice = minuteData.LowPrice,
                OpenPrice = minuteData.OpenPrice,
                KnockAmt = minuteData.KnockAmt,
                KnockAvgPrice = minuteData.KnockAvgPrice,
                KnockQty = minuteData.KnockQty,
                OccurTime = Quotation.Time.FromHMM(minuteData.OccurTime),
                TickSN = minuteData.TickSN
            };
        }

        public ComboMinuteData Convert(Quotation.Combo.MinuteData minuteData)
        {
            return new ComboMinuteData()
            {
                Code = minuteData.StkId.LongCode,
                ClosePrice = minuteData.ClosePrice,
                HighPrice = minuteData.HighPrice,
                LowPrice = minuteData.LowPrice,
                OpenPrice = minuteData.OpenPrice,
                KnockAmt = minuteData.KnockAmt,
                KnockAvgPrice = minuteData.KnockAvgPrice,
                KnockQty = minuteData.KnockQty,
                OccurTime = (ushort)minuteData.OccurTime.ToHMM(),
                TickSN = minuteData.TickSN
            };
        }
    }
}
